using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Threading;
namespace MicroMsg.Common.Utils
{
	public static class Log
	{
		private const string TAG = "Log";
		public const int LEVEL_VERBOSE = 0;
		public const int LEVEL_DEBUG = 1;
		public const int LEVEL_INFO = 2;
		public const int LEVEL_WARNING = 3;
		public const int LEVEL_ERROR = 4;
		public const int LEVEL_NONE = 5;
		private const int STRING_LOG_BUFFER_SIZE = 524288;
		private const string LOG_FILENAME = "/Log/MicroMsg.log";
		private const string BAK_FILENAME = "/Log/MicroMsg.bak";
		private const int INTERVAL_CHECK_LOG = 30000;
		private static int level = 1;
		private static bool writeToFile = true;
		private static bool bFlushFile = false;
		public static bool sBIsWriteToLogView = true;
		public static int MAX_FILE_LEN = 3145728;
		private static List<string> logCache = new List<string>(10);
		private static Thread m_thread = null;
		private static AutoResetEvent logEvent = new AutoResetEvent(false);
		private static AutoResetEvent logFlushOK = new AutoResetEvent(false);
		public static int Level
		{
			set
			{
				Log.level = value;
				Log.print(" -a ", "Log", "--------------new log begin, level: " + value + "------------------\n");
			}
		}
		public static bool OutputToFile
		{
			set
			{
				Log.writeToFile = value;
				if (Log.writeToFile && Log.m_thread == null)
				{
					Log.m_thread = new Thread(new ThreadStart(Log.LogThreadRun));
					Log.m_thread.Start();
				}
			}
		}
		public static void smethod_0(string TAG, object object_0)
		{
		}
		public static void smethod_1(string TAG, object object_0)
		{
			if (Log.level <= 4)
			{
				Log.print(" -e ", TAG, object_0);
			}
		}
		public static void smethod_2(string TAG, object object_0, Exception exception_0)
		{
			if (Log.level <= 4)
			{
				Log.print(" -e ", TAG, object_0 + ((exception_0 != null) ? (exception_0.GetType().get_FullName() + ":" + exception_0.get_Message()) : ""));
			}
		}
		public static void smethod_3(string TAG, object object_0)
		{
			if (Log.level <= 3)
			{
				Log.print(" -w ", TAG, object_0);
			}
		}
		public static void smethod_4(string TAG, object object_0)
		{
			if (Log.level <= 2)
			{
				Log.print(" -i ", TAG, object_0);
			}
		}
		public static void smethod_5(string TAG, object object_0)
		{
			if (Log.level <= 1)
			{
				Log.print(" -d ", TAG, object_0);
			}
		}
		[Conditional("LOG_V")]
		public static void smethod_6(string TAG, object object_0)
		{
			if (Log.level <= 0)
			{
				Log.print(" -v ", TAG, object_0);
			}
		}
		public static string[] strSplit(string string_0, int sub_len)
		{
			int num = string_0.get_Length() / sub_len;
			int num2 = string_0.get_Length() % sub_len;
			if (num2 != 0)
			{
				num++;
			}
			string[] array = new string[num];
			for (int i = 0; i < num; i++)
			{
				if (num2 != 0 && i == num - 1)
				{
					array[i] = string_0.Substring(i * sub_len, num2);
					return array;
				}
				array[i] = string_0.Substring(i * sub_len, sub_len);
			}
			return array;
		}
		private static void print(string level, string context, object object_0)
		{
			string text = string.Concat(new object[]
			{
				DateTime.get_Now().ToString("dd-HH:mm:ss:fff "),
				Thread.get_CurrentThread().get_ManagedThreadId().ToString(),
				level,
				"/",
				context.PadRight(15).Substring(0, 15),
				": ",
				object_0
			});
			text += "\n";
			if (!Log.writeToFile)
			{
				return;
			}
			lock (Log.logCache)
			{
				Log.logCache.Add(text);
				if (Log.logCache.get_Count() >= 40)
				{
					Log.logEvent.Set();
				}
			}
		}
		private static void LogThreadRun()
		{
			string text = "";
			try
			{
				IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
				userStoreForApplication.CreateDirectory("Log");
				IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("/Log/MicroMsg.log", 6, 2, 3, userStoreForApplication);
				StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream);
				if (streamWriter != null)
				{
					while (true)
					{
						Log.logEvent.WaitOne();
						lock (Log.logCache)
						{
							if (Log.logCache.get_Count() <= 0)
							{
								goto IL_11E;
							}
							for (int i = 0; i < Log.logCache.get_Count(); i++)
							{
								text += Log.logCache.get_Item(i);
							}
							Log.logCache.Clear();
						}
						goto IL_9D;
						IL_11E:
						if (Log.writeToFile)
						{
							continue;
						}
						break;
						IL_9D:
						if (isolatedStorageFileStream.get_Length() > (long)Log.MAX_FILE_LEN)
						{
							streamWriter.Close();
							isolatedStorageFileStream.Close();
							if (userStoreForApplication.FileExists("/Log/MicroMsg.bak"))
							{
								userStoreForApplication.DeleteFile("/Log/MicroMsg.bak");
							}
							userStoreForApplication.MoveFile("/Log/MicroMsg.log", "/Log/MicroMsg.bak");
							isolatedStorageFileStream = new IsolatedStorageFileStream("/Log/MicroMsg.log", 6, userStoreForApplication);
							streamWriter = new StreamWriter(isolatedStorageFileStream);
						}
						streamWriter.Write(text);
						if (Log.bFlushFile)
						{
							streamWriter.Flush();
							Log.bFlushFile = false;
						}
						text = "";
						Log.logFlushOK.Set();
						goto IL_11E;
					}
					streamWriter.Close();
					isolatedStorageFileStream.Close();
					Log.m_thread = null;
				}
			}
			catch (Exception ex)
			{
				Console.get_Out().Write(ex.get_Message());
				Log.m_thread = null;
			}
		}
		public static void smethod_7()
		{
			Log.forceFlush();
		}
		public static void forceFlush()
		{
			if (Log.writeToFile && Log.m_thread != null)
			{
				Log.print(" -a ", "LOG", "-------------- log force flush!!!");
				Log.logFlushOK.Reset();
				Log.bFlushFile = true;
				Log.logEvent.Set();
				Log.logFlushOK.WaitOne();
				return;
			}
		}
		public static void testLog()
		{
			Log.smethod_0("class2", "this is message");
			Log.smethod_1("class2", "this is error");
			Exception exception_ = new Exception("test exception");
			Log.smethod_2("class2", "this is error with exception message", exception_);
			Log.smethod_3("class2", "this is warn");
			Log.smethod_4("class2", "this is info ");
			Log.smethod_5("class2", "this is debug");
		}
		public static string getLogString()
		{
			return Log.getLogString(524288);
		}
		public static string getLogString(int maxLen)
		{
			Log.forceFlush();
			return Log.readCrashLog(maxLen);
		}
		private static string readCrashLog(int read_len)
		{
			string result = "";
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (userStoreForApplication.FileExists("/Log/MicroMsg.log"))
					{
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("/Log/MicroMsg.log", 3, 1, 3, userStoreForApplication))
						{
							int num = (isolatedStorageFileStream.get_Length() > (long)read_len) ? ((int)isolatedStorageFileStream.get_Length() - read_len) : 0;
							isolatedStorageFileStream.Seek((long)num, 0);
							using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream))
							{
								result = streamReader.ReadToEnd();
							}
						}
					}
				}
			}
			catch (Exception object_)
			{
				Log.smethod_1("Log", object_);
			}
			return result;
		}
	}
}
